// 页面中初始化
import * as Utils from '@/utils/util.js';
export default {
    name: 'mxInitWxConfig',
    data() {
        return {
            isSharePage: 0,
            wxExternalUserid: '', // 当前外部联系人userid
            wxUserId: '', // 当前客户经理userid
            chatId: '', // 当前客户群的群聊ID
            entry: '', // 页面入口环境
            wxJsApiList: [
                'agentConfig',
                'getContext',
                'sendChatMessage',
                'openEnterpriseChat',
                'getCurExternalUserId',
                'selectExternalContact',
                'shareToExternalContact',
                'getCurExternalContact',
                'launchMiniprogram',
                'getCurExternalChat',
                'chooseImage',
                'uploadImage',
                'hideChatAttachmentMenu',
                'onMenuShareAppMessage',
                'onMenuShareWechat',
                'navigateToAddCustomer',
                'shareToExternalMoments'
            ],
            agentJsApiList: [
                'getContext',
                'sendChatMessage',
                'openEnterpriseChat',
                'getCurExternalUserId',
                'selectExternalContact',
                'shareToExternalContact',
                'getCurExternalContact',
                'launchMiniprogram',
                'getCurExternalChat',
                'chooseImage',
                'uploadImage',
                'hideChatAttachmentMenu',
                'onMenuShareAppMessage',
                'onMenuShareWechat',
                'navigateToAddCustomer',
                'shareToExternalMoments'
            ]
        };
    },
    created() {
        if (Utils.isComWx() && this.$store.state.vuex_isConfig) {
            // 当前页面在企业微信环境中
            this.initWxConfigCom();
            this.$store.commit('updateVuexIsConfig', false);
        }
    },
    watch: {
        '$store.state.vuex_isSharePage': function () {
            this.isSharePage = this.$store.state.vuex_isSharePage;
        },
        '$store.state.vuex_wxUserId': function () {
            this.wxUserId = this.$store.state.vuex_wxUserId;
            this.$store.commit('updateVuexIsShowPage', false);
        },
        '$store.state.vuex_wxExternalUserid': function () {
            this.wxExternalUserid = this.$store.state.vuex_wxExternalUserid;
        },
        '$store.state.vuex_chatId': function () {
            this.chatId = this.$store.state.vuex_chatId;
        }
    },
    activated() {
        this.isSharePage = this.$store.state.vuex_isSharePage || this.isSharePage;
        this.wxUserId = this.$store.state.vuex_wxUserId || this.wxUserId;
        this.wxExternalUserid = this.$store.state.vuex_wxExternalUserid || this.wxExternalUserid;
        this.chatId = this.$store.state.vuex_chatId || this.chatId;
    },
    mounted() {},
    methods: {
        // 企业微信中初始化
        initWxConfigCom() {
            this.$wxApi.wxConfig(this.wxJsApiList).then((res) => {
                this.$wxApi.wxAgentConfig(this.agentJsApiList).then((res) => {
                    let wxUserId = localStorage.getItem('wxUserId') || '';
                    if (wxUserId) {
                        this.wxUserId = wxUserId; //成员id
                        this.$store.commit('setWxUserId', wxUserId);
                    }
                    if (!this.wxUserId) {
                        let appId = res.appId;
                        Utils.getWxUserId(appId, (res) => {
                            let wxUserId = res.wxUserId;
                            this.wxUserId = wxUserId; //成员id
                            this.$store.commit('setWxUserId', wxUserId);
                            localStorage.setItem('wxUserId', wxUserId);
                        });
                    }
                    // 优先判断页面的入口环境
                    this.$wxApi.getContext().then((entry) => {
                        this.entry = entry;
                        this.$store.commit('setEntry', this.entry);
                        if ((entry === 'single_chat_tools' || entry == 'contact_profile') && !this.wxExternalUserid) {
                            // 获取当前外部联系人userid(客户userid)
                            this.$wxApi.getCurExternalUserId().then((externalUserId) => {
                                this.wxExternalUserid = externalUserId;
                                this.$store.commit('setWxExternalUserid', externalUserId);
                            });
                        } else if ((entry == 'group_chat_tools' || entry == 'group_chat_tools') && !this.chatId) {
                            this.$wxApi.getCurExternalChat().then((chatId) => {
                                this.chatId = chatId;
                                this.$store.commit('setChatId', chatId);
                            });
                        }
                        if (entry == 'chat_attachment') {
                            this.$wxApi.hideChatAttachmentMenu().then(() => {
                                // this.wxExternalUserid = externalUserId;
                            });
                        }
                    });
                    // 隐藏右上角菜单接口
                    // wx.hideOptionMenu();
                    // wx.showMenuItems({
                    //   menuList: ["menuItem:share:appMessage", "menuItem:share:wechat",] // 要显示的菜单项
                    // });
                });
            });
        }
    }
};
